<template>
  <section class="todoapp">
    <!-- 头部部分 -->
    <HmHeader @addList="addList" />

    <!-- 主体部分 -->
    <HmBoday :list="redater" @delList="delList" @modify="modify" />

    <!-- 底部部分 -->
    <HmFoodaer
      v-show="list.length"
      :residue="residue"
      :type="type"
      :listAll="listAll"
      @screen="screen"
      @cleanup="cleanup"
    />
  </section>
</template>

<script>
import HmHeader from "./components/HmHeader.vue";
import HmBoday from "./components/HmBody.vue";
import HmFoodaer from "./components/HmFoodaer.vue";
export default {
  components: {
    HmHeader,
    HmBoday,
    HmFoodaer,
  },
  data() {
    return {
      list: JSON.parse(localStorage.getItem("list")) || [
        { id: 1, name: "吃饭", isDone: true },
        { id: 2, name: "睡觉", isDone: false },
        { id: 3, name: "打豆豆", isDone: true },
      ],
      type: "all",
    };
  },

  methods: {
    // 删除
    delList(id) {
      this.list = this.list.filter((item) => item.id !== id);
    },

    //添加
    addList(data) {
      const obj = { id: +new Date(), name: data, isDone: false };
      this.list = [obj, ...this.list];
    },

    // 修改
    modify(id) {
      const arr = this.list.find((item) => item.id === id);
      arr.isDone = !arr.isDone;
    },

    // 筛选
    screen(data) {
      // console.log(data);
      this.type = data;
    },

    cleanup() {
      this.list = this.list.filter((item) => !item.isDone);
    },
  },

  computed: {
    residue() {
      return this.list.filter((item) => !item.isDone).length;
    },

    listAll() {
      return this.list.some((item) => item.isDone === true);
    },

    redater() {
      switch (this.type) {
        case "all":
          return this.list;
          break;
        case "active":
          return this.list.filter((item) => !item.isDone);
          break;
        case "completed":
          return this.list.filter((item) => item.isDone);
          break;

        default:
          return this.list;
          break;
      }
    },
  },

  watch:{
    list:{
      deep:true,
      handler(){
        localStorage.setItem('list',JSON.stringify(this.list))
      }
    },
  },
};
</script>

<style></style>
